/**
 *  JFTSTool - Java Full Text Search tool.
 *  Copyright (C) <2009>  <grupo JFTS>
 *  This program is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.jftstool.fileParserFramework.metadata;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author grupo JFTS
 *
 */
public class DocumentMetaData implements IDocumentMetaDataConstants, CreativeCommons, DublinCore, HttpHeaders {
	
	private Map<String, String> metadata = null;
	
	/**
	 * 
	 */
	public DocumentMetaData() {
		metadata = new HashMap<String, String>();
	}
	
	/**
	 * Return metadata propertie.
	 * @return String[]
	 */
	public String[] getProperties() {
	    return metadata.keySet().toArray(new String[metadata.keySet().size()]);
	}
	
	/**
	 * Get property
	 * @param property
	 * @return String
	 */
	public String getProperty(final String property) {
	    String aux = null; 
	    aux = metadata.get(property);
	    return aux;
	  }
		
	/**
	 * Set propertie
	 * @param property
	 * @param value
	 */
	public void set(String property, String value) {
		if (value != null){
			metadata.put(property, value);
		}
	}
	
	
	/**
	 * 
	 * @param property
	 * @param value
	 */
	public void set(String property, Date value) {
		if (value != null){
			metadata.put(property, value.toString());
		}
	}
	

	/**
	 * 
	 * @param property
	 * @param value
	 */
	public void set(String property, Calendar value) {
		if (value != null){
			metadata.put(property, value.getTime().toString());
		}
	}
	

	/**
	 * 
	 * @param property
	 * @param value
	 */
	public void set(String property, long value) {
		metadata.put(property, Long.toString(value));
	}	
	
	
	
	/**
	 * Add property
	 * @param property
	 * @param value
	 */
	public void add(final String property, final String value) {
		String aux = metadata.get(property);
		if (aux == null) {
			metadata.put(property, value);
		}
	}
		
	/**
	 * 
	 * @param name
	 */
	public void remove(String name) {
	    metadata.remove(name);
	  }	
	
	/**
	 * Return size
	 * @return int
	 */
	 public int size() {
		 return metadata.size();
	 }
 
	public String toString() {
		StringBuffer buf = new StringBuffer();
	    String[] properties = this.getProperties();
	    for (int i = 0; i < properties.length; i++) {
	    	String value = this.getProperty(properties[i]);
	        buf.append(properties[i]).append(" = ").append(value).append("\n");
	    }
	    return buf.toString();
	  }
		
	public boolean equals(Object o) {
		if (o == null) {
			return false;
		}
		
	    DocumentMetaData other = null;
	    try {
	      other = (DocumentMetaData) o;
	    }
	    catch (ClassCastException cce) {
	    	return false;
	    }

	    if (other.size() != size()) {
	    	return false;
		}

	    String[] properties = this.getProperties();
	    for (int i = 0; i < properties.length; i++) {
	    	String otherProperty = other.getProperty(properties[i]);
	    	String thisProperty = this.getProperty(properties[i]);
	        if (!otherProperty.equals(thisProperty)) {
	          return false;
	        }
	    }
	    return true;
	}
	
	public void clear(){
		metadata.clear();
	}
}